package com.sh.wy.system.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.sh.util.RedisConstant;
import com.sh.util.Result;
import com.sh.wy.system.entity.LiveUser;
import com.sh.wy.system.entity.SysUser;
import com.sh.wy.system.service.LoginService;
import com.sh.wy.system.vo.LoginParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: 世豪学java
 * @Date: 2023/12/17 - 12 - 17 - 12:43
 * @Decsription: com.sh.wy.web.system.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/")
@Api(tags = "登录处理类")
public class LoginController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginService loginService;

    @PostMapping("/captcha")
    @ApiOperation("验证码处理")
    public Result captcha(){
        //1.获取验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 40, 4, 11);
        //获得验证码压缩图片
        String imageBase64 = lineCaptcha.getImageBase64();
        //拼接前缀，让浏览器识别
        imageBase64 = "data:image/png;base64,"+imageBase64;
        //获取验证码内容
        String captchaCode = lineCaptcha.getCode();
        //获取验证码id
        String captchaId = UUID.randomUUID().toString();

        //2.将验证码存到redis中
        redisTemplate.opsForValue().set(
                RedisConstant.CAPTCHA_PRE+captchaId,
                captchaCode,
                RedisConstant.CAPTCHA_EXPIRE_TIME,
                TimeUnit.SECONDS);

        //3.响应数据
        Map<String,Object> map = new HashMap<>();
        map.put("captchaId",captchaId);
        map.put("imageBase64",imageBase64);
        return Result.success(map);
    }

    @ApiOperation("登录请求处理")
    @PostMapping("/login")
    public Result login(
            @RequestBody
            @ApiParam(value = "登录需要参数",required = true)
            @Valid LoginParam loginParam){
        return loginService.login(loginParam);
    }

    @GetMapping("/hello")
    public Result hello(){
        return Result.success("hello");
    }

    @PostMapping("/logout")
    @ApiOperation("安全退出")
    public Result logout(){
        return loginService.logout();
    }

    //获取用户信息
    @GetMapping("/getInfo")
    @ApiOperation("查看个人信息")
    public Result getInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof SysUser){
            SysUser sysUser = (SysUser)principal;
            return Result.success(sysUser);
        }else{
            LiveUser liveUser = (LiveUser)principal;
            return Result.success(liveUser);
        }
    }
}
